package com.wm.action;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sun.mail.iap.Response;
import com.wm.bean.Restaurant;
import com.wm.bean.User;
import com.wm.service.RestaurantService;
import com.wm.service.UserService;
import com.wm.vo.RestaurantListVO;

import cn.itcast.service.PersonService;
import com.wm.vo.*;
public class UserAction extends DispatchAction {
	@Resource UserService userService;


	/**
	 * 注册
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward register (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String name = request.getParameter("tbnikename");
		String password = request.getParameter("tbAgain");
		String email = request.getParameter("tbEmail");
		
		System.out.println("houtai------------");System.out.println(name);System.out.println(password);System.out.println(email);
		
		User user = new User();
		String uuid = java.util.UUID.randomUUID().toString(); 
		user.setUid(uuid);
		user.setName(name);
		user.setPassword(password);
		user.setEmail(email);
		HttpSession session = request.getSession(); 

		if(!userService.register(user)){
			//返回前台，提示用户名已存在
			request.setAttribute("error", "用户名已存在");
			
			return mapping.findForward("error");
		}else{
			session.setAttribute("user", user);
			
			return mapping.findForward("registerSuccess");
		}
	} 
	
	/**
	 * 检测用户名是否存在
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public void checkname (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
			throws Exception {/*
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
				this.getServlet().getServletContext());
		PersonService personService =(PersonService)ctx.getBean("personService");*/
		System.out.println("11111111111111");
		String name = request.getParameter("name");
		
		User user = new User();
		user.setName(name);
		HttpSession session = request.getSession(); 
		System.out.println("2222222");
		//返回前台，提示用户名已存在
		response.setContentType("text/xml;charset=UTF-8");  
		 response.setHeader("Cache-Control", "no-cache");   
		PrintWriter returnValue=response.getWriter();   
		String output="";   //处理接收到的参数，生成响应的XML文档
		if(userService.checkname(name)){
			
			
			 output="用户名已存在";   
			   
		}else {
			output="success";  
		}
		returnValue.print(output);   
		 returnValue.close(); 
	}
	/**
	 * 登录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward login (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("action");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		
		User user = new User();
		user.setName(name);
		user.setPassword(password);
		HttpSession session = request.getSession(); 

		response.setContentType("text/xml;charset=UTF-8");  
		 response.setHeader("Cache-Control", "no-cache");   
		PrintWriter returnValue=response.getWriter();   
		String output="";   //处理接收到的参数，生成响应的XML文档
		
		if(!userService.login(user)){
			
			//返回前台，提示用户名或密码错误
			
			 output="<h class='faile'>用户名或密码错误</h>";   
			 System.out.println(output);
			 returnValue.print(output);   
			 returnValue.close();   
			return null;
		}else {
			session.setAttribute("user", user);
			output="success";   
			System.out.println(output);
			 returnValue.print(output);   
			 returnValue.close();  
			
			return null;
		}
		/*session.setAttribute("user", user);
		return mapping.findForward("loginSuccess");*/
		
	} 
	/** 
	 * 
	 * */
	public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		HttpSession session = request.getSession(); 
		session.setAttribute("user", null);
		String uid = (String)session.getAttribute("uid");
		response.sendRedirect("index.jsp?uid="+uid);
		return null;
	}
}
